'''
Created on 01-06-2012

@author: Magda
'''

from PyQt4 import QtCore, QtGui
from pl.edu.budgetanalizer.GUI.databaseManagment.producers import EditProducer
from pl.edu.budgetanalizer.model.Producer import Producer

import pyodbc

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s


class Ui_Form(QtGui.QWidget):
    
    listOfProducers = []
    
    def getAllProducers(self):
        '''
        Pobiera wszystkich producentow z bazy
        '''
        self.listOfProducers = []
        self.deleteComboBox.clear()
        self.editComboBox.clear()
        
        conn = pyodbc.connect('DRIVER={SQL Server};SERVER=MAGDAK\SQLEXPRESS;DATABASE=Baza;Trusted_Connection=yes')
        cur = conn.cursor()
        cur.execute("select * from Producers")
        rows = cur.fetchall()
        for row in rows:
            producer = Producer(row.name)
            self.listOfProducers.append(producer)
            self.deleteComboBox.addItem(producer.name)
            self.editComboBox.addItem(producer.name)
        conn.close()
    
    def delete(self):
        '''
        usuwa danego producenta z bazy
        '''
        conn = pyodbc.connect('DRIVER={SQL Server};SERVER=MAGDAK\SQLEXPRESS;DATABASE=Baza;Trusted_Connection=yes')
        cur = conn.cursor()
        try:
            cur.execute("DELETE FROM Producers WHERE name=?",str(self.deleteComboBox.currentText()))
            conn.commit()
        except pyodbc.IntegrityError:
            dialog = QtGui.QDialog()
            dialog.resize(358, 132)
            dialog.setGeometry(QtCore.QRect(50,50,100,100))
            label = QtGui.QLabel(dialog)
            label.setText("Nie mozna dokonac tej operacji - blad integracji")
            dialog.show()
            
        conn.close()
        self.getAllProducers()

    
    def add(self):
        '''
        dodaje danego producenta do bazy
        '''
        conn = pyodbc.connect('DRIVER={SQL Server};SERVER=MAGDAK\SQLEXPRESS;DATABASE=Baza;Trusted_Connection=yes')
        cur = conn.cursor()
        cur.execute("exec addProducer ?",str(self.lineEdit.text()))
        conn.commit()
        conn.close()
        self.lineEdit.setText("")
        self.getAllProducers()
        
        
    def openEdit(self):
        producer = Producer(self.editComboBox.currentText())
        self.producerManagmentWidget = EditProducer.Ui_Form()
        self.producerManagmentWidget.setupUi(self.producerManagmentWidget,producer,self.Form,self.databaseDir)
        self.producerManagmentWidget.setGeometry(QtCore.QRect(0, 0, 400, 120))
        self.producerManagmentWidget.setAutoFillBackground(True)
        self.producerManagmentWidget.setGeometry(QtCore.QRect(520,330,400,120))
        self.producerManagmentWidget.show()
        
    
    def setupUi(self, Form,databaseDir):
        self.Form = Form
        
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(410, 411)
        self.databaseDir = databaseDir
        
        #add
        self.addLabel = QtGui.QLabel(Form)
        self.addLabel.setGeometry(QtCore.QRect(20, 30, 361, 21))
        self.addLabel.setObjectName(_fromUtf8("addLabel"))
        self.lineEdit = QtGui.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(20, 90, 211, 20))
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.nameLabel = QtGui.QLabel(Form)
        self.nameLabel.setGeometry(QtCore.QRect(20, 70, 46, 13))
        self.nameLabel.setObjectName(_fromUtf8("nameLabel"))
        self.saveButton = QtGui.QPushButton(Form)
        self.saveButton.setGeometry(QtCore.QRect(310, 90, 75, 23))
        self.saveButton.setObjectName(_fromUtf8("saveButton"))
        self.saveButton.connect(self.saveButton, QtCore.SIGNAL("clicked()"),self.add)
        
        #edit
        self.editLabel = QtGui.QLabel(Form)
        self.editLabel.setGeometry(QtCore.QRect(20, 150, 361, 21))
        self.editLabel.setObjectName(_fromUtf8("editLabel"))
        self.editComboBox = QtGui.QComboBox(Form)
        self.editComboBox.setGeometry(QtCore.QRect(20, 190, 211, 22))
        self.editComboBox.setObjectName(_fromUtf8("editComboBox"))
        self.editButton = QtGui.QPushButton(Form)
        self.editButton.setGeometry(QtCore.QRect(310, 190, 75, 23))
        self.editButton.setObjectName(_fromUtf8("editButton"))
        self.editButton.connect(self.editButton, QtCore.SIGNAL("clicked()"),self.openEdit)
        
        #delete
        self.deleteLabel = QtGui.QLabel(Form)
        self.deleteLabel.setGeometry(QtCore.QRect(20, 270, 361, 21))
        self.deleteLabel.setObjectName(_fromUtf8("deleteLabel"))
        self.deleteComboBox = QtGui.QComboBox(Form)
        self.deleteComboBox.setGeometry(QtCore.QRect(20, 310, 211, 22))
        self.deleteComboBox.setObjectName(_fromUtf8("deleteComboBox"))
        self.deleteButton = QtGui.QPushButton(Form)
        self.deleteButton.setGeometry(QtCore.QRect(310, 310, 75, 23))
        self.deleteButton.setObjectName(_fromUtf8("deleteButton"))
        self.deleteButton.connect(self.deleteButton, QtCore.SIGNAL("clicked()"),self.delete)

        self.getAllProducers()
        
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.addLabel.setText(QtGui.QApplication.translate("Form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">Dodaj nowego producenta:</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.editLabel.setText(QtGui.QApplication.translate("Form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">Edytuj istniejacego producenta:</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.deleteLabel.setText(QtGui.QApplication.translate("Form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">Usun istniejacego producenta:</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.nameLabel.setText(QtGui.QApplication.translate("Form", "Nazwa:", None, QtGui.QApplication.UnicodeUTF8))
        self.saveButton.setText(QtGui.QApplication.translate("Form", "Dodaj", None, QtGui.QApplication.UnicodeUTF8))
        self.editButton.setText(QtGui.QApplication.translate("Form", "Edytuj", None, QtGui.QApplication.UnicodeUTF8))
        self.deleteButton.setText(QtGui.QApplication.translate("Form", "Usun", None, QtGui.QApplication.UnicodeUTF8))
    